{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d850998",
   "metadata": {},
   "source": [
    "# 01 kNN 基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88a43c9f",
   "metadata": {},
   "source": [
    "KNN可以说是最简单的分类算法之一，同时，它也是最常用的分类算法之一，注意KNN算法是监督学习中的分类算法。KNN的原理就是当预测一个新的值x的时候，根据它距离最近的K个点是什么类别来判断x属于哪个类别。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "404eddfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5d07b79",
   "metadata": {},
   "source": [
    "## 实现我们自己的 kNN"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebdc9d7f",
   "metadata": {},
   "source": [
    "### 创建简单测试用例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "61063a13",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_X = [[3.393533211, 2.331273381],\n",
    "              [3.110073483, 1.781539638],\n",
    "              [1.343808831, 3.368360954],\n",
    "              [3.582294042, 4.679179110],\n",
    "              [2.280362439, 2.866990263],\n",
    "              [7.423436942, 4.696522875],\n",
    "              [5.745051997, 3.533989803],\n",
    "              [9.172168622, 2.511101045],\n",
    "              [7.792783481, 3.424088941],\n",
    "              [7.939820817, 0.791637231]\n",
    "             ]\n",
    "raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "72e0149b",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = np.array(raw_data_X)\n",
    "y_train = np.array(raw_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7882814d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1318415b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 2)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9a31a4d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.39353321, 3.11007348, 1.34380883, 3.58229404, 2.28036244])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[y_train==0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "825f9ad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1ccadc99730>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAUZ0lEQVR4nO3df2zkd53f8edrE1dgfmxOF7ek2aznKqFTCxEQWQEaCUUsrQgk0Fb8EWRA4h8XGlWhPQkVLIHyh/+odEIrQCJyCYWIKYgCh0iUnC4KcJA/EupdkmzCIjW9xste0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q/azRUX1V9oqoOVVWH3o/n36uqPbHHlOQV/S+46S9n/Et6PxY3rqr+D/DzJH/cHzoCNP4F/AbvZw8tt/SdAt6SZLL/mT1C7/uuPSHJP+zfHwb+DRfp76+xi0Qn+RpwI3BlktPAp6vqrqbyrHMD8EHgRH+tGuCTVXVfc5HOuQr4Sv+IgwPAN6pqTx0euEf9I+DPep97Lgf+W1X9ebORzvPvgW5/aeOvgA83nOec/hrwvwD+bdNZ1quqR5J8EzhObznjJ+ytXwPwrSR/CJwBbquq/3cx3tRT/yWpJVxykaSWsNAlqSUsdElqCQtdklrCQpeklrDQJaklLHRJaon/D5C3KRENNGKbAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color=\"g\")\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color=\"r\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35b9f9eb",
   "metadata": {},
   "source": [
    "预测:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "673c8f11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1ccadd085e0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAUlElEQVR4nO3df2zkd53f8edrE1dgfmxOF7ek2aynldDpChEQWQEaCUVZWhFIoK34I8iAxD9uaFSF9iRUsATKH/6j0gmtACmRm1AuYhp0BQ4RlJwOBSjwR8J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3A8ar6v00HWefdwP+uqpWqehH4BvDPG850VlXdU1XXVNW76C0t7/r6OVjor9D/4PEe4GRVfa7pPOslmUpyWf/xq+n9o/5Zo6H6qupTVXWoqjr0fj3/blXtiT2mJK/pf8BNfznjX9L7tbhxVfV/gJ8n+YP+0BGg8Q/gN/gQe2i5pe8U8I4kk/2f2SP0Pu/aE5L8w/79YeDfcIH+/hq7SHSS+4DrgcuTnAY+W1X3NJVnneuAjwAn+mvVAJ+uqgeai3TWFcCf9I84OAD8aVXtqcMD96h/BPxZ7+eeS4H/VlV/3mykc/x7oNtf2vgr4GMN5zmrvwb8L4B/23SW9arqkSRfA47TW874CXvrawC+nuT3gReB26rq/12IN/XUf0lqCZdcJKklLHRJagkLXZJawkKXpJaw0CWpJSx0SWoJC12SWuL/AzeSRxEb0QRrAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.array([8.093607318, 3.365731514])\n",
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')\n",
    "plt.scatter(x[0], x[1], color='b')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ecbe0fe",
   "metadata": {},
   "source": [
    "### kNN的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2ffcb146",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "distances = []\n",
    "for x_train in X_train:\n",
    "    d = sqrt(np.sum((x_train - x)**2))\n",
    "    distances.append(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bee93318",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "07945f05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances = [sqrt(np.sum((x_train - x)**2)) for x_train in X_train]\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "099c9d3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5, 6, 9, 3, 0, 1, 4, 2], dtype=int64)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nearest = np.argsort(distances)\n",
    "nearest"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95c3d41a",
   "metadata": {},
   "source": [
    "获取排序后索引数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bda1f43a",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9cd3b3c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 1, 1, 0]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topK_y = [y_train[i] for i in nearest[:k]]\n",
    "topK_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1bd781b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({1: 5, 0: 1})"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "votes = Counter(topK_y)\n",
    "votes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fa92603",
   "metadata": {},
   "source": [
    "Counter函数用来统计给定数组的元素出现次数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27a6d367",
   "metadata": {},
   "source": [
    "获取投票结果票数最多的，使用most_common函数，传递2表示找到投票结果里面票数前2位的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dcfaac62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 5), (0, 1)]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes.most_common(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6e27e3a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y = votes.most_common(1)[0][0]\n",
    "predict_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee26740c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
